﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace DoDoveFramework
{
    public abstract class BaseBuilder<TTargetClass, TBuilder>
        where TTargetClass : new()
        where TBuilder : BaseBuilder<TTargetClass, TBuilder>
    {
        private readonly List<Func<TTargetClass, TTargetClass>> _actions = new List<Func<TTargetClass, TTargetClass>>();

        public TBuilder Do(Action<TTargetClass> action)=>AddAction(action);
        
        private TBuilder AddAction(Action<TTargetClass> _action)
        {
            _actions.Add(target =>
            {
                _action(target);
                return target;
            });
            return (TBuilder) this;
        }
        
        public TTargetClass Build()
        {
            return _actions.Aggregate(new TTargetClass(), (target, f) => f(target));
        }
    }
}